package com.lefu.es.system;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.Switch;

import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.fitness.FitnessOptions;
import com.google.android.gms.fitness.data.DataType;
import com.lefu.es.util.LogUtils;
import com.lefu.es.util.SPUtil;
import com.wellness.scale.R;

public class GoogFitActivity extends AppCompatActivity {
    private String TAG = "GoogFitActivity";
    private static final int REQUEST_OAUTH_REQUEST_CODE = 1;
    private LinearLayout layout_back;
    private boolean isGoogleFitOpen = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goog_fit);
        layout_back = (LinearLayout) findViewById(R.id.layout_back);
        Switch switch_google_fit = (Switch) findViewById(R.id.switch_google_fit);
        isGoogleFitOpen = (Boolean) SPUtil.get(GoogFitActivity.this, "isGoogleFitOpen", false);

        switch_google_fit.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    LogUtils.d(TAG, "打开Google Fit");
                    SPUtil.put(GoogFitActivity.this, "isGoogleFitOpen", true);
                    init();
                } else {
                    LogUtils.d(TAG, "关闭Google Fit");
                    SPUtil.put(GoogFitActivity.this, "isGoogleFitOpen", false);
                    /*try {
                        GoogleSignInAccount googleSignInAccount = GoogleSignIn.getLastSignedInAccount(GoogFitActivity.this);
                        if (googleSignInAccount != null) {
                            LogUtils.d(TAG, "关闭Google Fit");
                            Fitness.getConfigClient(GoogFitActivity.this, googleSignInAccount).disableFit();

                            GoogleSignInAccount googleSignInAccount1 = GoogleSignIn.getLastSignedInAccount(GoogFitActivity.this);
                            LogUtils.d(TAG, "googleSignInAccount1:" + googleSignInAccount1);

                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }*/

                }
            }
        });

        layout_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        if (!checkPlayServices()) {
            switch_google_fit.setEnabled(false);
        } else {
            GoogleSignInAccount googleSignInAccount = GoogleSignIn.getLastSignedInAccount(this);
            boolean isSignIn = false;
            if (googleSignInAccount != null) {
                isSignIn = GoogleSignIn.hasPermissions(googleSignInAccount);
                LogUtils.d(TAG, "===========isSignIn:" + isSignIn);
            }
            if (isSignIn && isGoogleFitOpen) {
                switch_google_fit.setChecked(true);
            } else {
                switch_google_fit.setChecked(false);
            }
        }

    }

    FitnessOptions fitnessOptions =
            FitnessOptions.builder()
                    .addDataType(DataType.TYPE_WEIGHT, FitnessOptions.ACCESS_WRITE)
                    .build();

    public void init() {
        if (!GoogleSignIn.hasPermissions(GoogleSignIn.getLastSignedInAccount(this), fitnessOptions)) {
            GoogleSignIn.requestPermissions(
                    this,
                    REQUEST_OAUTH_REQUEST_CODE,
                    GoogleSignIn.getLastSignedInAccount(this),
                    fitnessOptions);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == REQUEST_OAUTH_REQUEST_CODE) {

            }
        }
    }


    int PLAY_SERVICES_RESOLUTION_REQUEST = 2;

    private boolean checkPlayServices() {
        int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
        if (resultCode != ConnectionResult.SUCCESS) {
            if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
                GooglePlayServicesUtil.getErrorDialog(resultCode, this,
                        PLAY_SERVICES_RESOLUTION_REQUEST).show();
            } else {
                Log.i(TAG, "This device is not supported.");
                finish();
            }
            return false;
        }
        return true;
    }
}
